package com.song.controller.admin;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.song.pojo.Dept;
import com.song.service.DeptService;
import com.song.service.EmployeeService;
import com.song.utils.DataGridViewResult;
import com.song.utils.SystemConstant;
import com.song.vo.DeptVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 宋超
 * HP - the login name of the current user.
 * 2020/12/6 - the current system date.
 * 11:42 - the current system time.
 */
@RestController
@RequestMapping("/admin/dept")
public class DeptController {

    @Autowired
    private DeptService deptService;

    @Autowired
    private EmployeeService employeeService;


    //遍历部门信息
    @RequestMapping("/list")
    public DataGridViewResult list(DeptVo deptVo){
        //设置分页信息
        PageHelper.startPage(deptVo.getPage(),deptVo.getLimit());
        //调用分页查询的方法
        List<Dept> deptList = deptService.findDeptListByPage(deptVo);
        //创建分页对象
        PageInfo<Dept> pageInfo = new PageInfo<Dept>(deptList);
        //返回数据
        return new DataGridViewResult(pageInfo.getTotal(),pageInfo.getList());
    }

    //添加部门信息
    @RequestMapping("/addDept")
    public String addDept(Dept dept){
        //创建map存放信息
        Map<String,Object> map = new HashMap<String, Object>();
        int addDept = deptService.addDept(dept);
        if(addDept>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"添加成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"添加失败");
        }
        //map中存放的信息已json形式返回
        return JSON.toJSONString(map);
    }

    //修改部门信息
    @RequestMapping("/updateDept")
    public String updateDept(Dept dept){
        Map<String,Object> map = new HashMap<String, Object>();
        int updateDept = deptService.updateDept(dept);
        if(updateDept>0){
            map.put(SystemConstant.SUCCESS,true);
            map.put(SystemConstant.MESSAGE,"修改成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);
            map.put(SystemConstant.MESSAGE,"修改失败");
        }
        //map中存放的信息已json形式返回
        return JSON.toJSONString(map);
    }

    //查询该部门下是否有员工
    @RequestMapping("/checkDeptHasEmployee")
    public String checkDeptHasEmployee(Integer id){
        Map<String,Object> map = new HashMap<String, Object>();
        int countById = employeeService.getEmployeeCountById(id);
        if (countById>0){
            //该部门下存在员工
            map.put(SystemConstant.EXISI,true);//存在
            map.put(SystemConstant.MESSAGE,"该部门下存在员工,不能删除");
        }else{
            map.put(SystemConstant.EXISI,false);//不存在
        }
        return JSON.toJSONString(map);
    }

    //删除部门信息
    @RequestMapping("/deleteById")
    public String deleteById(Integer id){
        Map<String,Object> map = new HashMap<String, Object>();
        int deleteById = deptService.deleteById(id);
        if (deleteById>0){
            map.put(SystemConstant.SUCCESS,true);//删除成功
            map.put(SystemConstant.MESSAGE,"删除成功");
        }else{
            map.put(SystemConstant.SUCCESS,false);//删除失败
            map.put(SystemConstant.MESSAGE,"删除失败");
        }
        return JSON.toJSONString(map);
    }

    //查询所有部门
    @RequestMapping("/deptList")
    public String deptList(){
        return JSON.toJSONString(deptService.findDeptList());
    }
}
